package com.zxbit.flash.ui.activity.auth;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.zxbit.flash.R;
import com.zxbit.flash.base.BaseTitleActivity;
import com.zxbit.flash.common.AuthRouter;
import com.zxbit.flash.common.Constant;
import com.zxbit.flash.common.RouterMap;
import com.zxbit.flash.common.UserInfo;
import com.zxbit.flash.mvpview.DictionaryMvpView;
import com.zxbit.flash.mvpview.MoneyAuthMvpView;
import com.zxbit.flash.presenter.DictionaryPresenter;
import com.zxbit.flash.presenter.MoneyAuthPresenter;
import com.zxbit.flash.bean.AuthInfoData;
import com.zxbit.flash.response.AuthInfoResponse;
import com.zxbit.flash.response.BlankResponse;
import com.zxbit.flash.bean.DictionaryItemData;
import com.zxbit.flash.utils.ToastUtils;
import com.zxbit.flash.view.dialog.MenuDialog;

import java.util.ArrayList;
import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(name = "借款金额认证", path = RouterMap.MONEY_AUTH_PAGE)
public class MoneyAuthActivity extends BaseTitleActivity implements MoneyAuthMvpView, DictionaryMvpView {

    // 需要多少钱
    private final String DICTIONARY_TYPE_NEED_MONEY = "need_money";
    // 借款用途
    private final String DICTIONARY_TYPE_USAGE_OF_LOAN = "usage_of_loan";
    @BindView(R.id.tv_need_money)
    TextView tvNeedMoney;
    @BindView(R.id.et_need_money)
    EditText etNeedMoney;
    @BindView(R.id.rl_need_money)
    RelativeLayout rlNeedMoney;
    @BindView(R.id.tv_usage)
    TextView tvUsage;
    @BindView(R.id.et_usage)
    EditText etUsage;
    @BindView(R.id.rl_usage)
    RelativeLayout rlUsage;
    @BindView(R.id.tv_next_step)
    TextView tvNextStep;

    @Autowired(name = Constant.IS_BACK)
    boolean is_back;
    private ArrayList<DictionaryItemData> mNeedMoneyList;
    private ArrayList<DictionaryItemData> mUsageList;
    private MoneyAuthPresenter moneyAuthPresenter;
    private DictionaryPresenter dictionaryPresenter;
    private MenuDialog mMenuDialog;

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }


    @Override
    protected void onUIReady() {
        ARouter.getInstance().inject(this);

        moneyAuthPresenter = new MoneyAuthPresenter(this);
        dictionaryPresenter = new DictionaryPresenter(this);
        String[] dicts = {DICTIONARY_TYPE_NEED_MONEY, DICTIONARY_TYPE_USAGE_OF_LOAN};
        dictionaryPresenter.getDictionaryData(Arrays.asList(dicts));
        if (UserInfo.isShowUserInformation()) moneyAuthPresenter.getAuthInfo();
        if (is_back) {
            tvNextStep.setText(R.string.save_change);
        }
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_auth_money;
    }

    @Override
    public String getPageTitle() {
        return getStringResource(R.string.money_auth_money_title);
    }

    @Override
    public void onGetDataNext(AuthInfoResponse response) {
        fillTextView(response.getData().getBorrowingNeeds());
    }

    @Override
    public void onGetDataNext(BlankResponse response) {
        dismissLoadingView();
        if (response.getStatus() == Constant.HTTP_OK) {
            AuthRouter.getInstance().jumpNextPager(this, RouterMap.MONEY_AUTH_PAGE);
        } else {
            ToastUtils.showShort(this, response.getMessage());
        }
    }

    @Override
    public void onGetDataError(Throwable throwable) {
        dismissLoadingView();
    }

    @Override
    public void onGetDataNext(ArrayList<ArrayList<DictionaryItemData>> responseWrapper) {
        if (responseWrapper.size() >= 2) {
            mNeedMoneyList = responseWrapper.get(0);
            mUsageList = responseWrapper.get(1);
        }

    }

    public void fillTextView(AuthInfoData.BorrowingNeedsBean borrowingNeedsBean) {
        etNeedMoney.setText(borrowingNeedsBean.getNeedMoneyValue());
        etNeedMoney.setTag(borrowingNeedsBean.getNeedMoney());
        etUsage.setText(borrowingNeedsBean.getMoneyToDoValue());
        etUsage.setTag(borrowingNeedsBean.getMoneyTodo());

    }


    public MenuDialog getMenuDialog() {
        if (mMenuDialog == null) {
            mMenuDialog = new MenuDialog(this, R.style.dialog_bottom_full);
        }
        return mMenuDialog;
    }

    private void picker_need_money() {
        if (mNeedMoneyList == null || mNeedMoneyList.size() == 0) {
            return;
        }
        getMenuDialog().
                showDialog(etNeedMoney, mNeedMoneyList);
    }

    private void picker_usage() {
        if (mUsageList == null || mUsageList.size() == 0) {
            return;
        }
        getMenuDialog().
                showDialog(etUsage, mUsageList);
    }

    @OnClick({R.id.rl_need_money, R.id.et_need_money, R.id.rl_usage, R.id.et_usage, R.id
            .tv_next_step})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_need_money:
            case R.id.et_need_money:
                picker_need_money();
                break;

            case R.id.rl_usage:
            case R.id.et_usage:
                picker_usage();
                break;

            case R.id.tv_next_step:
                commit();
                break;
            default:
                break;
        }

    }

    private void commit() {
        String authPageCode = AuthRouter.getInstance().routerMap2Code(RouterMap.MONEY_AUTH_PAGE);
        String mNeedMoney = (String) etNeedMoney.getTag();
        String mUsage = (String) etUsage.getTag();
        if (TextUtils.isEmpty(mNeedMoney)) {
            ToastUtils.showShort(this, R.string.money_auth_need_money_hint);
            return;
        } else if (TextUtils.isEmpty(mUsage)) {
            ToastUtils.showShort(this, R.string.money_auth_usage_hint);
            return;
        }

        moneyAuthPresenter.saveAuthInfo(mNeedMoney, mUsage, authPageCode);
        showLoadingView();

    }

}
